PALETTE, PALETTE USING Statements ---------------------------------------------------------------------------- Action Change one or more colors in the palette. Syntax PALETTE attribute%, color& PALETTE USING array-name ( array-index) Remarks The PALETTE and PALETTE USING statements use the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- attribute% The palette attribute to be changed. color& The display color number to be assigned to the attribute. The color& value must be a long-integer expression for the IBM Video Graphics Array adapter (VGA) and IBM Multicolor Graphics Array adapter (MCGA) in screen Argument Description ---------------------------------------------------------------------------- Array adapter (MCGA) in screen modes 11 to 13. Integer or long-integer expressions can be used with the IBM Enhanced Graphics Adapter (EGA). array-name An array that contains more than one display color. It must be a long-integer array for VGA and MCGA adapters in screen modes 11 to 13. Otherwise, it can be either an integer or long-integer array. array-index The index of the first array element to use in setting the palette. The arguments array-name and array-index are used to change Argument Description ---------------------------------------------------------------------------- array-index are used to change more than one palette assignment with a single PALETTE USING statement. The PALETTE statement works only on systems equipped with the EGA, VGA, or MCGA adapters. The PALETTE statement is not supported in screen modes 3 or 4. The PALETTE statement provides a way of mapping display colors (the actual binary values used by the adapter) to color attributes (a smaller set of values). When a program enters a screen mode, the attributes are set to a series of default color values. (See the SCREEN statement for a list of the default colors.) In the EGA, VGA, and MCGA adapters, the default values have been selected so the display shows the same colors, even though the EGA uses different color values. With the PALETTE statement you can assign different colors to the attributes. Changing the display color assigned to an attribute immediately changes those colors currently displayed on the screen associated with that attribute. For example, assume that the current palette contains colors 0, 1, 2, and 3 in the four attributes numbered 0, 1, 2, and 3. The following DRAW statement selects attribute 3, and draws a line of 100 pixels using the display color associated with attribute 3, in this case also 3. DRAW "C3L100" If the following statement is executed, the color associated with attribute 3 is changed to color 2. PALETTE 3,2 All text or graphics currently on the screen displayed using attribute 3, including the line that is 100 pixels long, are instantaneously changed to color 2. Text or graphics subsequently dis-played with attribute 3 also are displayed in color 2. The new palette of colors contains 0, 1, 2, and 2. A PALETTE statement with no arguments sets the palette back to the default color values. With PALETTE USING, all entries in the palette can be modified in one statement. Each attribute in the palette is assigned a corresponding color from the array. The dimensions for the array must be large enough to set all the palette entries after array-index. For example, if you are assigning colors to a palette with 16 attributes and the array-index argument is 5 (the first array element to use in resetting the palette), then the dimensions for the array must be declared to hold at least 20 elements (because the number of elements from 5 to 20, inclusive, is 16). DIM PAL%(20) . . . PALETTE USING PAL%(5) Note that a color& argument of -1 in the array leaves the attribute unchanged in the palette. All other negative numbers are invalid values for color. Attribute 0 is always the screen background color. Under a common initial palette setting, points colored with the attribute 0 appear black on the display screen. Using the PALETTE statement, you could, for example, change the mapping of attribute 0 from black to white. You can also use the COLOR statement to change the screen background color in modes 1 and 7 through 10. The 64 EGA colors are derived from four levels each of red, green, and blue. For example, black is composed of red, green, and blue levels of (0,0,0), bright white is (3,3,3), dark gray is (1,1,1), and so on. The best way to see the Microsoft BASIC color code (0-63) associated with each combination of red, green, and blue levels is to run the following program. ' Display the EGA color codes 1 through 63 ' using color code 0 (black) as background. DEFINT A-Z SCREEN 9 ' Establish EGA screen mode. ' Display a set of nine color bars. FOR ColorCode% = 1 TO 9 LINE (((ColorCode% * 64) - 24), 40)-STEP(60, 140), ColorCode%, BF NEXT ColorCode% ' Display seven sets of nine color bars. ' A new set is displayed each time user presses a key. FOR Set% = 0 TO 6 FOR ColorBar% = 1 TO 9 DisplayCode% = (Set% * 9) + ColorBar% LOCATE 15, (ColorBar% * 8) PRINT DisplayCode% PALETTE ColorBar%, DisplayCode% NEXT ColorBar% SLEEP NEXT Set% END The following table lists attribute and color ranges for various adapter types and screen modes. See the SCREEN statement for the list of colors available for various screen modes, monitor, and graphics-adapter combinations. ----------------------------------------------------------------------------- Screen Monitor Adapter Attribute Color PALETTE mode attached range range supported ---------------------------------------------------------------------------- 0 Monochrome MDPA 0 - 15 No Monochrome EGA 0 - 15 0 - 2 Yes Color CGA 0 - 15 No Color-Enha EGA 0 - 15 0 - 15 - 0 Yes nced1 - 63 Analog VGA 0 - 15 0 - 63 Yes Color-Anal MCGA 0 - 15 0 - 63 No og 1 Color CGA 0 - 3 0 - 152 No Screen Monitor Adapter Attribute Color PALETTE mode attached range range supported ---------------------------------------------------------------------------- Color-Enha EGA 0 - 3 0 - 15 Yes nced1 Analog VGA 0 - 3 0 - 15 Yes Color-Anal MCGA 0 - 1 0 - 15 No og 2 Color CGA 0 - 1 No Color-Enha EGA 0 - 1 0 - 15 Yes nced1 Analog VGA 0 - 1 0 - 15 Yes Color-Anal MCGA 0 - 1 0 - 15 No og Screen Monitor Adapter Attribute Color PALETTE mode attached range range supported ---------------------------------------------------------------------------- og 3 Monochrome HGC 0 - 1 No 4 Color-Enha OCGA- 0 - 1 0 - 153 No nced OEGA- OVGA 7 Color-Enha EGA 0 - 15 0 - 15 Yes nced1 VGA 0 - 15 0 - 15 Yes 8 Color-Enha EGA 0 - 15 0 - 15 Yes nced1 VGA 0 - 15 0 - 15 Yes 9 Enhanced1 EGA4 0 - 3 0 - 63 Yes Screen Monitor Adapter Attribute Color PALETTE mode attached range range supported ---------------------------------------------------------------------------- 9 Enhanced1 EGA4 0 - 3 0 - 63 Yes Enhanced1 EGA5 0 - 15 0 - 63 Yes Analog VGA 0 - 16 0 - 63 Yes 10 Monochrome EGA 0 - 3 0 - 8 Yes Analog VGA 0 - 3 0 - 8 Yes 11 Analog VGA 0 - 1 0 - Yes 262,1436 Analog MCGA 0 - 1 0 - Yes 262,1436 12 Analog VGA 0 - 15 0 - Yes 262,1436 Screen Monitor Adapter Attribute Color PALETTE mode attached range range supported ---------------------------------------------------------------------------- 262,1436 13 Analog VGA 0 - 255 0 - Yes 262,1436 Analog MCGA 0 - 255 0 - Yes 262,1436 1 IBM Enhanced Color Display. 2 Color range available for attribute 0 only. 3 Color range available for attribute 1 only. 4 With 64K of EGA memory. 5 With more than 64K of EGA memory. 6 Range of display colors is actually from 0 to 4,144,959, but only 262,144 of these can be displayed. To calculate a VGA color value, select the intensities of red, green, and blue. The intensity of a color is a number from 0 (low intensity) to 63 (high intensity). Then use the following formula to calculate the actual color number. color number = 65,536 * blue + 256 * green + red This formula yields integer values from 0 to 4,144,959, but because there are gaps in the range of color numbers, you should use the formula rather than just select a number. When used with the IBM Analog Monochrome Monitor, the VGA color values are converted to a gray-scale value by taking a weighted sum of the red, blue, and green intensities. gray value = 11% blue + 59% green + 30% red For example, if the blue, green, and red intensities are 45, 20, and 20, the gray intensity value calculation would be gray value = (.11 * 45) + (.59 * 20) + (.30 * 20) = 22. The fractional part of the result is dropped. See Also COLOR, SCREEN Statement Example The following example illustrates the use of the PALETTE and PALETTE USING statements. DEFINT A-Z DIM SHARED PaletteArray(15) CONST ASPECT = 1 - 3 SCREEN 8' 640 x 200, 16 color resolution. ' Initialize PaletteArray. FOR I = 0 TO 15 PaletteArray(I) = I NEXT I ' Draw and paint concentric ellipses. FOR ColorVal = 15 TO 1 STEP -1 Radius = 20 * ColorVal CIRCLE (320, 100), Radius, ColorVal, , , ASPECT PAINT (320, 100), ColorVal NEXT ' Shift the palette until a key is pressed. DO FOR I = 1 TO 15 PaletteArray(I) = (PaletteArray(I) MOD 15) + 1 NEXT I PALETTE USING PaletteArray(0) ' Map the black background to a random color. PALETTE 0, PaletteArray(INT(RND * 15)) LOOP WHILE INKEY$ = ""